package com.cyandpos.pad.morefunction;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.GridView;

/**
 * Created by zx on 2017/3/2.
 */

public class PayGridView extends GridView {
    //定义行数
    private int rownum;

    //实现构造方法
    public PayGridView(Context context) {
        super(context);
    }
    public PayGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public PayGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int colnum = getNumColumns(); //获取列数
        int total = getChildCount();  //获取Item总数
//        LocalLog.writeLog("获取的列数："+colnum
//        +"   获取的Item总数为："+total);

        //计算行数
        if (total % colnum == 0) {
            rownum = total / colnum;
        } else {
            rownum = (total / colnum) + 1; //当余数不为0时，要把结果加上1
        }
//        LocalLog.writeLog("获取的rownum："+rownum);
        Paint localPaint; //设置画笔
        localPaint = new Paint();
        localPaint.setStyle(Paint.Style.FILL_AND_STROKE); //画笔实心
       // localPaint.setColor(getContext().getResources().getColor(R.color.line));//画笔颜色

        View view0 = getChildAt(0); //第一个view

        View viewColLast;
        View viewRowLast;
        if(total ==1 || total==2 ){
            viewColLast = getChildAt(total - 1);//第一行最后一个view
        }else{
            viewColLast = getChildAt(colnum - 1);//第一行最后一个view
        }
        viewRowLast = getChildAt((rownum - 1) * colnum); //第一列最后一个view


       /* for (int i = 1, c = 1; i < rownum || c < colnum-1; i++, c++) {
            //画横线
            if(view0!=null)
                canvas.drawLine(view0.getLeft(), view0.getBottom() * i, viewColLast.getRight(), viewColLast.getBottom() * i, localPaint);
        }
        for (int i = 1, c = 1; i < rownum || c < colnum; i++, c++) {
            //画竖线
            if(view0!=null)
                canvas.drawLine(view0.getRight() * c, view0.getTop(), viewRowLast.getRight() * c, viewRowLast.getBottom(), localPaint);
        }*/


    }
}
